package com.gph.saviorframework.mq.api.bean;

import com.gph.saviorframework.mq.api.MQFactory;
import com.gph.saviorframework.mq.api.Message;
import com.gph.saviorframework.mq.api.SendResult;
import com.gph.saviorframework.mq.api.exception.MQClientException;
import com.gph.saviorframework.mq.api.order.OrderProducer;

import java.util.Properties;

public class OrderProducerBean implements OrderProducer {

    private Properties properties;

    private OrderProducer producer;
    /**
     * 启动服务
     */
    public void start() {
        if (null == this.properties) {
            throw new MQClientException("properties not set");
        }

        this.producer = MQFactory.createOrderProducer(this.properties);
        this.producer.start();
    }

    /**
     * 关闭服务
     */
    public void shutdown() {
        if (this.producer != null) {
            this.producer.shutdown();
        }
    }

    /**
     * 发送顺序消息
     *
     * @param message     消息
     * @param shardingKey 顺序消息选择因子
     * @return 发送结果，含消息Id
     */
    public SendResult send(Message message, String shardingKey) {
        return this.producer.send(message,shardingKey);
    }

    public Properties getProperties() {
        return properties;
    }


    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public boolean isStarted() {
        return this.producer.isStarted();
    }

    public boolean isClosed() {
        return this.producer.isClosed();
    }
}
